﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PPT = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.PowerPoint;
using AppUtils;

namespace PresentationUtils
{
    public static class PPTControl
    {
        private static PPT.Application oPPT = null;

        private delegate void Action();

        private static void DoAction(Action pa)
        {
            try
            {
                Logger.log(string.Format("DoAction: {0}", pa.ToString()));

                pa.Invoke();
            }
            catch (Exception e)
            {
                Logger.log(string.Format("Cannot 'DoAction': {0}", e.Message));
            }
        }

        private static PPT.Application getPPT()
        {
            if (oPPT != null)
            {
                return oPPT;
            }
            DoAction(delegate
            {
                // Create an instance of PowerPoint.
                oPPT = new PPT.ApplicationClass();
                // Show PowerPoint to the user.
                oPPT.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
                oPPT.Assistant.On = false;
                oPPT.WindowState = PPT.PpWindowState.ppWindowNormal;
            });
            return oPPT;
        }

        public static PPT.Application PowerPoint
        {
            get
            {
                return getPPT();
            }
        }

        public static PPT.Presentations Presentations
        {
            get
            {
                return getPPT().Presentations;
            }
        }

        public static IList<String> PresentationNames
        {
            get
            {
                IList<String> names = new List<String>();
                DoAction(delegate
                {
                    foreach (PPT.Presentation x in PPTControl.Presentations)
                    {
                        names.Add(string.Format("{0}-{1}", x.Name, x.FullName));
                    }
                });
                return names;
            }
        }
    }
}
